using System;
using System.Threading.Tasks;
using JlPay.Sdk.Core;
using JlPay.Sdk.Services.Openmerch.Device;
using JlPay.Sdk.Models.Openmerch.Device;

namespace JlPay.Sdk.Examples.Openmerch.Device
{
    /// <summary>
    /// 开放商户 - 设备添加功能使用示例
    /// </summary>
    public class DeviceAddDemo
    {
        /// <summary>
        /// 构建设备添加请求
        /// </summary>
        private static DeviceAddRequest BuildDeviceAddRequest()
        {
            var request = new DeviceAddRequest
            {
                MerchNo = "84944035812S123",
                DeviceType = "term",  // 设备类型
                DeviceNo = "NS301257964",  // 设备号
                PrintMerchName = "测试打印商户名称",  // 打印商户名称
                DetAddress = "北京市朝阳区xxx街道",  // 设备地址
                ShopId = "SHOP001"  // 门店ID
            };

            return request;
        }

        /// <summary>
        /// 设备添加示例
        /// </summary>
        public static async Task DeviceAddDemoAsync()
        {
            Console.WriteLine("开始执行设备添加示例");

            // 使用DemoConfig创建配置对象
            var config = DemoConfig.CreateConfigWithCrypto();
            Console.WriteLine("配置初始化完成，已启用自动加密和解密功能");

            var deviceService = new DeviceService(config);

            // 创建设备添加请求
            var request = BuildDeviceAddRequest();

            try
            {
                // 发送请求
                var response = await deviceService.AddDeviceAsync(request);

                // 使用优化的响应处理
                response.HandleResponse("设备添加", config);
            }
            catch (Exception ex)
            {
                // 使用优化的异常处理
                var response = new DeviceAddResponse();
                response.HandleResponseWithException("设备添加", config, ex);
            }

            Console.WriteLine("设备添加示例执行完毕");
        }

        /// <summary>
        /// 主程序入口
        /// </summary>
        public static async Task Main(string[] args)
        {
            Console.WriteLine("=" + new string('=', 50));
            Console.WriteLine("===== 设备添加示例 开始 =====");
            Console.WriteLine("=" + new string('=', 50));

            await DeviceAddDemoAsync();

            Console.WriteLine("=" + new string('=', 50));
            Console.WriteLine("===== 设备添加示例执行完毕 =====");
            Console.WriteLine("=" + new string('=', 50));
        }
    }
} 